﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Manager_SysAdmin : System.Web.UI.PageEx
{
    private string strPageCode = "S0000B";
    private string strLoginResult = string.Empty;

    private int iPageCount = 0;
    private int iRecordCount = 0;
    private string strQuery = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            DataBinds();
            btnReset.OnClientClick = sfAdmin.GetResetReference();
        }
    }

    private void DataBinds()
    {
        DataSet ds = WebBase.Comm.SelectByPage("dbManager ", "*", strQuery, "mId", "mId", 0, 1, gridList.PageSize, gridList.PageIndex + 1, out iPageCount, out iRecordCount);
        gridList.RecordCount = iRecordCount;
        gridList.DataSource = ds;
        gridList.DataBind();
    }
    protected void btnAddAdmin_Click(object sender, EventArgs e)
    {
        winAdmin.Popup = true;
    }
    protected void gridList_RowCommand(object sender, FineUI.GridCommandEventArgs e)
    {
        Dictionary<int, Dictionary<int, string>> modifiedDict = gridList.GetModifiedDict();
        int iSelectId = 0;
        string strCommandName = e.CommandName.ToString();
        if (!string.IsNullOrEmpty(strCommandName))
        {
            int iSelectIndexId = e.RowIndex;
            string strSelectId = gridList.Rows[iSelectIndexId].DataKeys[0].ToString();
            Int32.TryParse(strSelectId, out iSelectId);
            switch (strCommandName)
            {
                case "Modify":
                    if (modifiedDict.ContainsKey(iSelectIndexId))
                    {
                        Dictionary<int, string> rowDict = modifiedDict[iSelectIndexId];
                        Model.DbManager manager = DalEx.DbManagerEx.Select(iSelectId);
                        if (manager != null)
                        {
                            if (rowDict.ContainsKey(1))
                            {
                                manager.MPwd = SqlHelper.MD5(SqlHelper.String(rowDict[1]), 32);
                            }
                            if (rowDict.ContainsKey(4))
                            {
                                manager.MStatus = Convert.ToInt16(Convert.ToBoolean(rowDict[4]));
                            }
                            if (DalEx.DbManagerEx.Update(manager) > 0)
                            {
                                DataBinds();

                                Model.DbActionLog actionLog = new Model.DbActionLog();
                                actionLog.AUId = UserId.ToString();
                                actionLog.ADateTime = DateTime.Now;
                                actionLog.ANote = "帐户ID(" + iSelectId + ")资料已修改";
                                actionLog.AIpaddress = WebBase.GetClientIP.getIPAddress2Num().ToString();
                                actionLog.AOS = WebBase.Comm.GetOS;
                                actionLog.ABrowse = WebBase.Comm.GetUserBrowser;
                                Dal.DbActionLogDal.Insert(actionLog);

                                FineUI.Alert.Show("帐户资料已修改", FineUI.MessageBoxIcon.Information);
                            }
                        }
                    }
                    else
                    {
                        FineUI.Alert.Show("您没有修改任何信息不能保存！", FineUI.MessageBoxIcon.Warning);
                    }
                    break;
                case "Delete":
                    if (iSelectId == UserId)
                    {
                        FineUI.Alert.Show("抱歉，您不能删除自身帐户！", FineUI.MessageBoxIcon.Warning);
                    }
                    else
                    {
                        DalEx.DbManagerEx.Delete(iSelectId);


                        Model.DbActionLog actionLog = new Model.DbActionLog();
                        actionLog.AUId = UserId.ToString();
                        actionLog.ADateTime = DateTime.Now;
                        actionLog.ANote = "帐户ID(" + iSelectId + ")资料已删除";
                        actionLog.AIpaddress = WebBase.GetClientIP.getIPAddress2Num().ToString();
                        actionLog.AOS = WebBase.Comm.GetOS;
                        actionLog.ABrowse = WebBase.Comm.GetUserBrowser;
                        Dal.DbActionLogDal.Insert(actionLog);

                        DataBinds();
                        FineUI.Alert.Show("帐户资料已删除", FineUI.MessageBoxIcon.Information);
                    }
                    break;
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string strUserName = SqlHelper.String(txtUserName.Text.Trim());
        string strUserPwd = SqlHelper.String(txtUserPwd.Text.Trim());
        string strReUserPwd = SqlHelper.String(txtReUserPwd.Text.Trim());
        Int16 iStatus = Convert.ToInt16(cboxStatus.Checked);
        if (strUserPwd != strReUserPwd)
        {
            FineUI.Alert.Show("抱歉，您输入的两次密码不一致！", FineUI.MessageBoxIcon.Warning);
        }
        else
        {
            Model.DbManager manager = new Model.DbManager();
            manager.MUser = strUserName;
            manager.MPwd = SqlHelper.MD5(strUserPwd, 32);
            manager.MLoginTimes = 0;
            manager.MLastIP = WebBase.GetClientIP.getIPAddress2Num("127.0.0.1").ToString();
            manager.MLastDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ;
            manager.MRoleCode = 10000;
            manager.MStatus = iStatus;

            string strResult = string.Empty;
            if (DalEx.DbManagerEx.InsertEx(manager, ref strResult) > 0)
            {

                Model.DbActionLog actionLog = new Model.DbActionLog();
                actionLog.AUId = UserId.ToString();
                actionLog.ADateTime = DateTime.Now;
                actionLog.ANote = strResult;
                actionLog.AIpaddress = WebBase.GetClientIP.getIPAddress2Num().ToString();
                actionLog.AOS = WebBase.Comm.GetOS;
                actionLog.ABrowse = WebBase.Comm.GetUserBrowser;
                Dal.DbActionLogDal.Insert(actionLog);

                DataBinds();
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
            }
            else
            {
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Warning);
            }
        }
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        int iPageSize = 20;
        int.TryParse(ddlPageSize.SelectedValue, out iPageSize);
        gridList.PageSize = iPageSize;
        DataBinds();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        DataBinds();
    }
}
